package com.spring.web.adminmanage.repository;

import com.spring.web.adminmanage.entities.Menu;
import com.spring.web.adminmanage.entities.MenuItem;
import com.spring.web.adminmanage.myMapper.MyMapper;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * Created by chenwenhao on 2017/6/28.
 * 菜单操作
 */
@Mapper
public interface MenuDao extends MyMapper<Menu> {

    @Select("SELECT * FROM menu where pid=#{pid} order by scort asc")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "name", column = "name"),
            @Result(property = "url", column = "url"),
            @Result(property = "scort", column = "scort"),
            @Result(property = "menuItem", column = "id",javaType = List.class, many = @Many(select="com.spring.web.adminmanage.repository.MenuDao.getMenuItemByPId"))
    })
    List<Menu> getAllMenu(String pid);

    @Select("SELECT * FROM menu where pid = #{pid} order by scort asc")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "name", column = "name"),
            @Result(property = "url", column = "url"),
            @Result(property = "scort", column = "scort")
    })
    List<MenuItem> getMenuItemByPId(@Param("pId") String pId);


    @Select("select * from menu t \n" +
            "\tLEFT JOIN role_menu n on  t.id = n.menuid\n" +
            "\tLEFT JOIN role r on n.roleid = r.id\n" +
            "\twhere r.id = #{roleId} order by t.scort asc")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "name", column = "name"),
            @Result(property = "url", column = "url"),
            @Result(property = "scort", column = "scort")
    })
    List<Menu> getMenuByRoleId(String roleId);



}
